package com.yang.service.wximpl;

import com.github.pagehelper.PageInfo;
import com.yang.bean.MarketAddress;
import com.yang.bean.MarketAddressExample;
import com.yang.bean.common.CommonData;
import com.yang.mapper.MarketAddressMapper;
import com.yang.service.wx.WXAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName WXAddressServiceImpl
 * @Description:
 * @Author TrueEnd
 * @Date 2022/9/11
 * @Version
 **/
@Service
public class WXAddressServiceImpl implements WXAddressService {


    @Autowired
    MarketAddressMapper marketAddressMapper;

    @Override
    public CommonData<MarketAddress> addressList(Integer userId) {
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);
        PageInfo pageInfo = new PageInfo(marketAddresses);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketAddress addressDetail(Integer id) {
        MarketAddress marketAddress = marketAddressMapper.selectByPrimaryKey(id);
        return marketAddress;
    }

    @Override
    public void addressSave(MarketAddress marketAddress,Integer userId) {
        if(marketAddress.getId()==0){
            marketAddress.setUserId(userId);
            marketAddressMapper.insertSelective(marketAddress);
        }
        else {
            marketAddress.setUserId(userId);
            marketAddressMapper.updateByPrimaryKey(marketAddress);
        }
    }

    @Override
    public void addressDelete(Integer id) {
        marketAddressMapper.deleteByPrimaryKey(id);

    }
}
